/**
 * Created by zycool on 2016/10/10.
 * 管道基础信息 - 基础数据
 */


;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管径
    var $diameter = $('#JQ_diameter').select3();
    DyyBase.ajax(baseDataApi.url_pipe_diameter(), {}, function (dataset) {
        var json = DyyBase.getSelect(dataset);
        json.shift();
        $diameter.add(json);
    });
    // 材质
    var $material = $('#JQ_material').select3({
        search: false
    });
    DyyBase.ajax(baseDataApi.url_material_class(), {}, function (dataset) {
        var json = DyyBase.getSelect(dataset);
        $material.add(json);
    });
    // 设计压力 min
    var $pressure_min = $('#JQ_pressure_min');
    // 设计压力 max
    var $pressure_max = $('#JQ_pressure_max');
    // 基础数据表格
    var $table_base = $('#JQ_table_base');
    // 导出
    var $export_base = $('#JQ_export_base');
    // 列表切换按钮
    var $list_span = $('#JQ_toggle_list_span');
    // 地图切换按钮
    var $map_span = $('#JQ_toggle_map_span');

    // 查询对象
    var filter = {
        getSearch: function (param) {
            var data = {};
            data.mode = true;
            if (param) {
                if (param.start == 0) {
                    data['jsonBean.pageNum'] = 1;
                } else {
                    data['jsonBean.pageNum'] = param.start / 10 + 1;
                }
                data['jsonBean.pageCount'] = param.length;
            }
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.pipeLineName = function () {
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            data.diameter = function () {
                var v = $diameter.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.material = function () {
                var v = $material.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.startPressureRating = function () {
                var v = $pressure_min.val();
                if (DyyBase.notEmpty(v)) {
                    if (v < 0 || v > 49) {
                        $pressure_min.TIP({content: '最小压力范围0~49'});
                        data.mode = false;
                    }
                    return v;
                }
                return '';
            }();
            data.endPressureRating = function () {
                var v = $pressure_max.val();
                if (DyyBase.notEmpty(v)) {
                    if (v < 0 || v > 50) {
                        $pressure_max.TIP({content: '最大压力范围0~50'});
                        data.mode = false;
                    }
                    return v;
                }
                return '';
            }();
            return data;
        },
        getExport: function(){
            return filter.getSearch();
        }
    };

    // 加载列表
    var DTBASE = $table_base.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSearch(data);
            DyyBase.ajax(baseDataApi.url_pipe_base_search(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {
            var th = $table_base.find('th');
            var empty = $table_base.find('td.dataTables_empty');
            empty.attr('colspan',th.length);
            if($map_span.hasClass('active')){
                $map_span.removeClass('active');
                DyyBase.showMap('map', $table_base, 'JQ_dyMap', [{name: 'gisId', type: 'pipeSegement'}]);
            }
        },
        'columns': [
            {
                data: 'pipeLineName', render: function (data, type, row) {
                return '<span title="' + data + '">' + data + '</span><input type="hidden" name="gisId" value="' + row.gisId + '">';
            }
            },
            {
                data: 'fromPipeValveRoomName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'toPipeValveRoomName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'designLength', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'diameter', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'wallthickness', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'material', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'pressureRating', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'id', render: function (data) {
                return '<i title="详情" class="zy-details-icon" data-id="' + data + '"></i>';
            }
            },
            {
                data: 'id', render: function (data, type, row) {
                return '<i title="高程" class="zy-altitude-icon" data-id="' + data + '"></i>';
            }
            }
        ]
    });

    // 详情
    $table_base.on('click', 'i.zy-details-icon', function () {
        DyyBase.showDetails($(this), DTBASE, $table_base, pipeTemplate['base']);
    });

    // 查询
    $('#JQ_search_base').click(function () {
        if (DTBASE) {
            DTBASE.draw();
        }
    });

    // 导出
    $export_base.click(function () {
        var param = (filter.getExport instanceof Function) ? filter.getExport() : filter.getExport;
        $(this).EXPT({
            type: 'name',
            name: param.pipeLineName + '管道基本信息表',
            param: param,
            server: baseDataApi.url_pipe_base_export()
        });
    });

    // 显示地图
    $map_span.click(function () {
        $export_base.hide();
        DyyBase.showMap('map', $table_base, 'JQ_dyMap', [{name: 'gisId', type: 'pipeSegement'}]);
    });

    // 显示列表
    $list_span.click(function () {
        $export_base.show();
        DyyBase.showMap('list', $table_base, 'JQ_dyMap', [{name: 'gisId', type: 'pipeSegement'}]);
    });

    // 跳转高程
    $table_base.on('click','i.zy-altitude-icon',function(){
        var $this = $(this);
        var $form = $('#JQ_jump_form').empty();
        var $tr = $this.parent('td').parent('tr');
        var data = DTBASE.row($tr).data();

        var post = {
            pipeLineId: data.pipeLineId,
            pipeLineName: data.pipeLineName,
            pipeSegmentId: data.pipeSegmentId,
            pipeSegmentName: data.pipeSegmentName,
            beginId: data.fromPipeValveRoomId,
            beginName: data.fromPipeValveRoomName,
            endId: data.toPipeValveRoomId,
            endName: data.toPipeValveRoomName
        };

        for(var k in post){
            var $input = $('<input type="hidden">');
            $input.prop('name', k).val(post[k]);
            $form.append($input);
        }

        $form.submit();
    });

})(jQuery, window, document);